from django.conf.urls import url
from rest_framework.routers import DefaultRouter

from users_1.decorator import my_decorator
from . import views

# urlpatterns是django自动识别的路由列表, 解析路由的方式是从上至下,匹配到需求的路由后就停止检索(即有可能上面的路由将屏蔽掉下面的路由)
urlpatterns = [
    # 每个路由信息都需要使用url函数来构造
    # url(正则匹配路径, 视图)
    url(r'^index/$', views.index, name='news_index'),
    url(r'^say', views.say),
    url(r'^weather/([a-z]+)/(\d{4})/$', views.weather),
    # url(r'^weather/(?P<city>[a-z]+)/(?P<year>\d{4})/$', views.weather)
    url(r'^qs/$', views.qs_params),
    url(r'^demo_view/$', views.demo_view),
    url(r'^demo_view_out/$', views.demo_view_out),
    # as_view()返回一个函数名view,view函数通过dispatch函数根据传入的请求方式来查找RegisterView类中的请求方式对应的小写形式方法名.
    url(r'^register/$', views.RegisterView.as_view(), name='register'),
    # 用自定义的装饰器对类视图进行装饰,这种方式不好,在类视图处看不出来,可在类视图函数处采取method-decorator装饰器使用那么参数指明被装饰的方法
    # url(r'^register/$',my_decorator(views.RegisterView.as_view())),
    url(r'template/$', views.template_simple),
    url(r'^createview/$', views.CreateView.as_view()),
    url(r'^selectview/$', views.SelectView.as_view()),
    url(r'^heroview/$', views.HeroView.as_view()),
    url(r'^editview/$', views.EditView.as_view()),
    url(r'^deleteview/$', views.DeleteView.as_view()),
    url(r'^books/$', views.BooksAPIView.as_view()),
    url(r'^book/(?P<pk>\d+)/$', views.BookAPIView.as_view()),
]

# 注册由REST框架(DRF)序列化/反序列化后的视图函数到路由中,DRF提供了一个当前路径的根路由,这里需要开启csrf防护
router = DefaultRouter()  # 可以处理视图的路由器
router.register(r'books_reset', views.BookInfoViewSet)  # 向路由器中注册视图集
urlpatterns += router.urls